<template>
    <div class="hello">
        <div id="status">{{ status }}</div>
        <button @click="requestFullScreen">进入考试</button>
        <button @click="exitFullScreen">退出考试</button>
        <button @click="removeListen">解除监听</button>
    </div>
</template>

<script>
let timer = null;
export default {
    name: "HelloWorld",
    props: {
        msg: String,
    },
    data() {
        return {
            status: 0,
        };
    },
    mounted() {
        this.listen()
    },

    methods: {
        check() {
            if (timer) clearTimeout(timer);
            timer = setTimeout(() => {
                if (!this.isFullScreen()) {
                    console.log("非全屏");
                    this.status = '非全屏'
                } else {
                    console.log("全屏");
                    this.status = '全屏'
                }
            }, 500);
        },
        listen() {
            window.addEventListener("resize", this.check);
        },
        //判断是否全屏
        isFullScreen() {
            return document.fullscreenElement && document.fullscreenElement !== null;
        },
        requestFullScreen() {
            const element = document.documentElement;
            if (element.requestFullscreen) {
                element.requestFullscreen();
            }
        },
        removeListen() {
            window.removeEventListener("resize", this.check);
            this.status = 0
        },
        exitFullScreen() {
            if (document.exitFullscreen) {
                document.exitFullscreen();
            }
        },
    },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
    margin: 40px 0 0;
}

ul {
    list-style-type: none;
    padding: 0;
}

li {
    display: inline-block;
    margin: 0 10px;
}

a {
    color: #42b983;
}
</style>
